package com.sinodata.ns.login;

import jt.classic.system.ISystem;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.log.LogManager;
import jt.classic.system.user.IUser;
import jt.classic.system.user.UserManager;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import org.limp.basework.impl.CommonBean4HSQ;

import org.limp.mine.StringTool;
import org.limp.mine.XMLTool;

import java.sql.Connection;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 手机登录控制器
 *
 * @author mido
 *
 */
public class Login4MobileAction extends DispatchAction {
    /**
     * 登录
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward login(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        /*String charsetName = (String) XSystemConfig.config().getProperty(XSystemConfig.XSYSTEM_CHARSETNAME);

        Map param = XMLTool.parseXML2Param(request, charsetName);*/
        CommonBean4HSQ cbh = new CommonBean4HSQ(request);

        Map param = cbh.getResource();

        String logid = StringTool.checkString(param.get("logid"));

        String pwd = StringTool.checkString(param.get("pwd"));

        String code = StringTool.checkString(param.get("mobilecode"));

        Connection conn = null;

        Map resultMap = new HashMap();
        resultMap.put("opera", "login");

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            UserManager um = new UserManager(conn);

            IUser user = um.logcheck(logid, pwd);

            if (user == null) {
                resultMap.put("status", "-1");
                resultMap.put("errMsg", "帐号或密码错误");
            } else if ((user != null) && (user.iflag() == -1)) {
                resultMap.put("status", "-2");
                resultMap.put("errMsg", "帐号已经被冻结");
            } else {
                /*SupervisorManager sm = new SupervisorManager(conn);

                ISupervisor sv = sm.getSupervisorByUserCode(user.id(), code);

                if (sv == null) {
                    resultMap.put("status", "-3");
                    resultMap.put("errMsg", "登录帐号与设备号不一致");
                } else {
                    resultMap.put("status", "0");
                    resultMap.put("userid", user.id());
                    resultMap.put("username", user.iname());
                    resultMap.put("groupname", user.igroup().iname());

                    LogManager lm = new LogManager(conn);

                    lm.log(user, request.getRemoteAddr(), "", "",
                        "mobileLogin", "insert",
                        "Supervisor('" + user.iname() + "','" + user.ilogid() +
                        "') log-in");

                    conn.commit();
                }*/
                resultMap.put("status", "0");
                resultMap.put("userid", user.id());
                resultMap.put("username", user.iname());
                resultMap.put("groupname", user.igroup().iname());
                resultMap.put("day", String.valueOf(new Date().getDate()));

                LogManager lm = new LogManager(conn);

                lm.log(user, request.getRemoteAddr(), "", "", "mobileLogin",
                    "insert",
                    "Supervisor('" + user.iname() + "','" + user.ilogid() +
                    "') log-in");

                conn.commit();
            }
        } catch (Exception ex) {
            resultMap.put("status", "-2");
            resultMap.put("errMsg", "登录服务调用异常,请与管理员联系");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        String result = XMLTool.result2XML(resultMap);

        request.setAttribute("result", result);

        return mapping.findForward("result");
    }
}
